//package com.example.farmer.View;
//
//import android.content.Intent;
//import android.graphics.Bitmap;
//import android.net.Uri;
//import android.os.Bundle;
//import android.provider.MediaStore;
//import android.transition.Slide;
//import android.view.View;
//import android.widget.Button;
//import android.widget.EditText;
//
//import androidx.activity.result.ActivityResult;
//import androidx.activity.result.ActivityResultCallback;
//import androidx.activity.result.ActivityResultLauncher;
//import androidx.activity.result.contract.ActivityResultContracts;
//import androidx.annotation.Nullable;
//import androidx.appcompat.app.AppCompatActivity;
//import androidx.recyclerview.widget.GridLayoutManager;
//import androidx.recyclerview.widget.RecyclerView;
//
//
//import com.example.farmer.R;
//
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//
//public class AddposterActivity extends AppCompatActivity implements
//        GoodsPhotoAdapter.OnPhotoListener, AddGoodsContract.IAddGoodsView, View.OnClickListener {
//
//   private RecyclerView mPhotoRecyclerView;
//   private EditText mGoodsNameEdit;
//   private Button mSendButton;
//   private List<Photo> mPhotoList;
//   private GoodsPhotoAdapter mGoodsPhotoAdapter;
//   private ActivityResultLauncher mActivityResultLauncher;
//   private AddGoodsContract.IAddGoodsPresenter mAddGoodsPresenter;
//
//   @Override
//   protected void onCreate(@Nullable Bundle savedInstanceState) {
//      super.onCreate(savedInstanceState);
//      getWindow().setEnterTransition(new Slide());
//      setContentView(R.layout.activity_add_poster);
//      mAddGoodsPresenter = new AddGoodsPresenter(this, this);
//      mPhotoList = new ArrayList<>();
//      mPhotoList.add(new Photo());
//      mPhotoRecyclerView = findViewById(R.id.rv_poster_photo);
//      mGoodsNameEdit = findViewById(R.id.et_poster_name);
//      mSendButton = findViewById(R.id.poster_send);
//      mSendButton.setOnClickListener(this);
//      GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
//      gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
//      mPhotoRecyclerView.setLayoutManager(gridLayoutManager);
//      mGoodsPhotoAdapter = new GoodsPhotoAdapter(mPhotoList);
//      mGoodsPhotoAdapter.setPhotoListener(this);
//      mPhotoRecyclerView.setAdapter(mGoodsPhotoAdapter);
//      onActivityResult();
//   }
//
//   @Override
//   public void onAdd() {
//      Intent intent = new Intent();
//      intent.setType("image/*");
//      intent.setAction(Intent.ACTION_GET_CONTENT);
//      mActivityResultLauncher.launch(intent);
//   }
//
//   @Override
//   public void onDelete(int position) {
//      //暂时没用
//   }
//
//   private void onActivityResult() {
//      mActivityResultLauncher = registerForActivityResult(
//              new ActivityResultContracts.StartActivityForResult(),
//              new ActivityResultCallback<ActivityResult>() {
//                 @Override
//                 public void onActivityResult(ActivityResult result) {
//                    int resultCode = result.getResultCode();
//                    if (resultCode == RESULT_OK) {
//                       Intent data = result.getData();
//                       Uri selectedImageUri = data.getData();
//                       try {
//                          // 使用图片URI进行处理，例如加载到ImageView中
//                          Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
//                          Photo photo = new Photo();
//                          photo.setBitmap(bitmap);
//                          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//                          Bitmap resized = Bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth()*0.1), (int)(bitmap.getHeight()*0.1), true);
//                          resized.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
//                          photo.setData(outputStream.toByteArray());
//                          mGoodsPhotoAdapter.addPhoto(photo);
//                       } catch (IOException e) {
//                          e.printStackTrace();
//                       }
//                    }
//                 }
//              });
//   }
//
//   @Override
//   public void showGoodsList(List<Goods> goodsList) {
//
//   }
//
//   @Override
//   public void onClick(View v) {
//      Goods goods = new Goods();
//      goods.setName(mGoodsNameEdit.getText().toString());
//      goods.setPrice(3.21);
//      goods.setUnit("两");
//      goods.setMessage("好吃好吃太好吃了");
//      goods.setPhotoList(mGoodsPhotoAdapter.getPhotoList());
//      mAddGoodsPresenter.saveGoods(goods);
//   }
//}
